/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.stm.main;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Popup;
import javafx.stage.Stage;
import sun.plugin.javascript.navig.Anchor;

/**
 *
 * @author ENOO
 */
public class LogIn implements Initializable {

    STMMainController stmMainCtr;
    Pane stmMainPane;
    @FXML
    private Label label;
    @FXML
    AnchorPane pane;
    @FXML
    TextField id;
    @FXML
    PasswordField pw;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Hello World!");
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        try {
            URL location = getClass().getResource("STMMain.fxml");

            ResourceBundle resources = ResourceBundle.getBundle("com.stm.main.bundle_ko_KR");
            FXMLLoader fxmlLoader = new FXMLLoader(location, resources);
            AnchorPane page = (AnchorPane) fxmlLoader.load();

            stmMainPane = (Pane) page;
            stmMainCtr = (STMMainController) fxmlLoader.getController();

        } catch (IOException ex) {
            Logger.getLogger(LogIn.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    public void logInOnMouseClicked(MouseEvent event) {
        System.out.println(stmMainPane);
        System.out.println(pane.getParent().getParent().getParent());
        TabPane tabPane = (TabPane) pane.getParent().getParent().getParent();
        stmMainCtr = (STMMainController) tabPane.getUserData();

        if (id.getText().equals("admin")) {
            if (pw.getText().equals("admin")) {
                stmMainCtr.login();
                return;
            }
        }

        Stage dialogStage = new Stage();
        dialogStage.initModality(Modality.WINDOW_MODAL);
        dialogStage.setScene(new Scene(VBoxBuilder.create().
                children(new Text("Log in failed. Check your ID or Password.")).
                alignment(Pos.CENTER).padding(new Insets(5)).build()));

        dialogStage.show();
    }
    
    public void pwOnKeyPressed(KeyEvent event){
       if(event.getCode().equals(KeyCode.ENTER)){
           logInOnMouseClicked(null);
       }
    }
}
